package com.xspace.android.xafcommon.view.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xspace.android.xafcommon.R;
import com.xspace.android.xafcommon.been.CommonItemBean;
import com.xspace.android.xafcommon.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Wubj 创建于 2017/2/24 0024.
 */
public class PopupWindowHelper {

    private static final String TAG = PopupWindowHelper.class.getSimpleName();

    public static final int LEFT = 0x100;
    public static final int CENTER = 0x010;

    private PopupWindow popupWindow;
    private Context mContext;
    private List<CommonItemBean> mList = new ArrayList<>();
    private ArrayAdapter adapter;
    private int placeMode = CENTER;

    public static PopupWindowHelper newInstance(Context mContext) {
        return new PopupWindowHelper(mContext);
    }

    private PopupWindowHelper(Context mContext) {
        this.mContext = mContext;
    }

    public PopupWindowHelper setDataList(final List<CommonItemBean> mList) {
        this.mList.clear();
        this.mList.addAll(mList);
        adapter = new ArrayAdapter<CommonItemBean>(mContext, R.layout.common_popup_layout_item, mList) {
            @NonNull
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = View.inflate(mContext, R.layout.common_popup_layout_item, null);
                }
                CommonItemBean bean = mList.get(position);
                LinearLayout topLine = (LinearLayout) convertView.findViewById(R.id.line_common_popup);
                if (position == 0) {
                    topLine.setVisibility(View.INVISIBLE);
                }
                TextView textView = (TextView) convertView.findViewById(R.id.tv_common_popup_title);
                TextView unreadCount = (TextView) convertView.findViewById(R.id.tv_unread_count);
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) textView.getLayoutParams();
                if (placeMode == CENTER) {
                    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
                }
                textView.setLayoutParams(layoutParams);
                textView.setText(bean.title);
                //只显示未读状态,不显示数字,则使用unreadStatus
                if (bean.unreadStatus) {
                    unreadCount.setVisibility(View.VISIBLE);
                } else {
                    unreadCount.setVisibility(View.GONE);
                }
                //第二种情况,用于显示未读数字
//                if (bean.unreadCount > 0) {
//                    unreadCount.setText(bean.unreadCount);
//                    unreadCount.setVisibility(View.VISIBLE);
//                } else {
//                    unreadCount.setText("");
//                    unreadCount.setVisibility(View.GONE);
//                }
                return convertView;
            }
        };
        return this;
    }

    /**
     * 设置布局展示模式,靠左还是居中
     *
     * @param mode {@link PopupWindowHelper} 靠左PopupWindowHelper.LEFT,居中时可以不用设置
     * @return
     */
    public PopupWindowHelper setPlaceMode(int mode) {
        placeMode = mode;
        return this;
    }

    /**
     * 刷新popupWindow列表信息
     */
    public void refreshPopup() {
        adapter.notifyDataSetChanged();
    }

    /**
     * 显示更多弹窗
     *
     * @param v
     */
    public void showPopup(View v) {
        popDismiss();
        int[] location = new int[2];
        v.getLocationInWindow(location);
        if (popupWindow == null) {
            View contentView = View.inflate(mContext, R.layout.common_popup_layout, null);
            ListView mListPop = (ListView) contentView.findViewById(R.id.list_pop);
            mListPop.setAdapter(adapter);
            mListPop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (listener != null) {
                        listener.onItemClick(view, mList.get(position), position);
                    }
                    popDismiss();
                }
            });
            popupWindow = new PopupWindow(contentView, -2, -2, true);
            // 必须设置背景颜色,动画效果才会生效
            popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

            LogUtil.d(TAG, "wubaojie>>>onRightClick: iconHight:" + v.getMeasuredHeight());
            LogUtil.d(TAG, "wubaojie>>>onRightClick: iconRight:" + v.getMeasuredWidth() / 2);
        }
        popupWindow.showAtLocation(v, Gravity.RIGHT + Gravity.TOP, v.getMeasuredWidth() / 2,
                location[1] + v.getMeasuredHeight());
    }

    public interface IPopupClickListener {
        void onItemClick(View view, CommonItemBean bean, int position);
    }

    private IPopupClickListener listener;

    public PopupWindowHelper setListener(IPopupClickListener listener) {
        this.listener = listener;
        return this;
    }

    /**
     * popupWindow的关闭方法
     */
    public void popDismiss() {
        if (popupWindow != null) {
            popupWindow.dismiss();
        }
    }
}
